import { ref, computed, type Ref } from 'vue'

export function useCheckbox<T>() {
  const options = ref<T[]>([]) as Ref<T[]>
  const checked = ref<T[]>([]) as Ref<T[]>

  const isCheckedAll = computed<boolean>(() => checked.value.length === options.value.length)
  const indeterminate = computed<boolean>(() => !isCheckedAll.value && checked.value.length > 0)

  const toggleIndeterminate = () => (checked.value = isCheckedAll.value ? [] : options.value)

  return {
    options,
    checked,
    isCheckedAll,
    indeterminate,

    toggleIndeterminate
  }
}
